home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / campbell / docu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  5.1 KB  |  222 lines

  1. #define EOF      -1
  2. #define MAXLINE 133
  3. #define TEN         100
  4. #define MAXTEN    13300
  5. #define SPACE     3
  6. #define CP_ int
  7.  
  8. #include <stdio.h>
  9.  
  10. main(argc, argv)
  11. int argc;
  12. CP_ argv;
  13. /*-
  14.    Routine to take stdin and create a document on stdout.  A "document"
  15.    is all comments lines starting with /*- and ending with
  16. */
  17. /*-
  18.    In addition, this program will also output up to 10 lines before the
  19.    /*- comment until a blank line is found (going in reverse up the document).
  20. */
  21. {
  22. /* Variables
  23.    comment- Value 0 means no comment seen yet, value 1 comment, value 2 end.
  24. */
  25.    int i, comment, top, end_of_file, empty, blanks, save, nlines;
  26.    char buffer[MAXTEN];
  27.  
  28. #ifdef VMS
  29.    redirect(&argc,argv,0); /*  - redirect standard I/O (from chj) */
  30. #endif
  31.  
  32.    if (argc != 1) useage();
  33.  
  34.    comment = 0;
  35.    empty   = 0;
  36.    save    = 0;
  37.    top     = TEN;
  38.  
  39.  
  40.    do {
  41.       end_of_file = fillup (buffer, MAXLINE, TEN, save, &nlines);
  42.    /* Cycle through the current buffer, locate top. */
  43.  
  44.       for (top = top + save - TEN, i = save; i < nlines; i++) {
  45.          test (buffer+MAXLINE*i, MAXLINE, &empty, &comment);
  46.          if (comment > 0) {
  47.             for (; top < i; top++)
  48.                putout (buffer+MAXLINE*top, MAXLINE);
  49.             top = TEN;
  50.             putout (buffer+MAXLINE*i, MAXLINE);
  51.             save = 0;
  52.          }
  53.          if (comment == 2) {
  54.               putspace (SPACE);
  55.               top = i+1;
  56.          }
  57.          if (comment == 0) save++;
  58.          if (empty && comment == 0) top = i + 1;
  59.  
  60.       /* Save the least number of lines necessary. */
  61.          save = TEN - top > save ? save : TEN - top;
  62.  
  63.       /* Be sure that at least one new line is read in. */
  64.          save = save >= TEN ? TEN - 1 : save;
  65.       }
  66.    } while (end_of_file != EOF);
  67.  
  68.    exit(0);
  69. }
  70.  
  71.  
  72. docopy (src, dst, max)
  73. char *src, *dst;
  74. int max;
  75. /*-
  76.    Routine to copy src to dst up to and including the '\n' character,
  77.    not more than max characters will be copied, however.
  78. -*/
  79. {
  80.    while (*src != '\n' && max--)
  81.       *dst++ = *src++;
  82.    *dst = '\n';
  83. }
  84.  
  85.  
  86. error (string)
  87. char *string;
  88. /*-
  89.    Routine to print a string and then die.
  90. -*/
  91. {
  92.    while (*string) putchar(*string++);
  93.    putchar('\n');
  94. }
  95.  
  96.  
  97. fillup (buffer, maxline, size, save, nlines)
  98. char *buffer;
  99. int maxline, size, save, *nlines;
  100. /*-
  101.    Routine to fill a buffer at offsets maxline for size lines from stdin.
  102.    The buffer will "slide" up by size-save number of lines (eg save = 4 means
  103.    that the last 4 lines will become the top 4 lines).
  104.  
  105.    Parameters:
  106.  
  107.        buffer: maxline*size character array.
  108.  
  109.       maxline: length of longest line (until a '\n' is seen).
  110.  
  111.          size: number of lines the buffer can hold.
  112.  
  113.          save: number of lines from previous buffer to save as top lines
  114.                of this new buffer.
  115.  
  116.        nlines: number of active lines in this buffer (returned).
  117. -*/
  118. {
  119.    int line, count;
  120.    char *ptr;
  121.  
  122.    line  = 0;
  123.    count = 0;
  124.  
  125. /* Save the bottom lines by rearranging them as the top lines. */
  126.    for (line = 0; line < save; line++)
  127.       docopy (buffer+maxline*(size-save+line), buffer+maxline*line, maxline);
  128.  
  129. /* Read in some more lines.  fgets fills in up to maxline characters. */
  130.    while (line < size && 
  131.          (ptr = fgets(&buffer[maxline*line++], maxline, stdin)) != NULL);
  132.  
  133. /* Return the number of active lines in the buffer. */
  134.    *nlines = line;
  135.    return (ptr == NULL ? EOF : 1);   /* eof tested in calling routine */
  136. }
  137.  
  138.  
  139. putout (buffer, maxline)
  140. char *buffer;
  141. int maxline;
  142. /*-
  143.    Routine to put the buffer out to stdout until either maxline is
  144.    reached or a newline is reached.
  145. -*/
  146. {
  147.    do {
  148.       putchar (*buffer);
  149.       if (*buffer++ == '\n')
  150.          return;
  151.    } while (maxline--);
  152. }
  153.  
  154.  
  155. putspace (space)
  156. int space;
  157. /*-
  158.    Routine to put space around each seperate comment block.
  159. -*/
  160. {
  161.    putchar ('\n');
  162.    while (space--)
  163.       putchar('-');
  164.    putchar ('\n');
  165. }
  166.  
  167.  
  168. test (buffer, maxline, empty, comment)
  169. char *buffer;
  170. int maxline, *empty, *comment;
  171. /*-
  172.    Routine to test buffer and determine if it is empty (all white space)
  173.    or if comment should toggle.  Comment is set to 1 when it is 0 and then
  174.    a run of /*- is seen.  Comment is set to 0 when it is 1 and a closing
  175.    'C' comment is seen, like this:
  176. */
  177. {
  178.    if (*comment == 2)
  179.       *comment = 0;     /* Turn off comment indicator */
  180.  
  181.    *empty = 1;
  182.    while (*buffer == ' ' || *buffer == '\t' || *buffer == '\n') {
  183.       maxline--;
  184.       if (*buffer == '\n' || maxline == 0)
  185.          return;
  186.       buffer++;
  187.    }
  188.    
  189. /* buffer now points at a non-blank character. */
  190.    *empty = 0;
  191.  
  192. /* Check to see if start of comment is anywhere on this line. */
  193.    while (*(buffer+1) != '\n' && maxline > 0) {
  194.       if (*buffer == '/' && *(buffer+1) == '*' && *(buffer+2) == '-')
  195.          *comment = 1;
  196.       if (*comment == 1 && *buffer == '*' && *(buffer+1) == '/')
  197.          *comment = 2;
  198.       buffer++;
  199.       maxline--;
  200.    }
  201. }
  202.  
  203.  
  204. useage()
  205. /*-
  206.    Explain the useage of docu:
  207.  
  208.    docu <stdin >stdout
  209.  
  210.    (strips document comments out of 'C' code)
  211. */
  212. {
  213.    static char *mess1 = "docu <stdin >stdout",
  214.                *mess2 = "(strips document comments out of 'C' code)";
  215.  
  216.    while (*mess1) putchar (*mess1++);
  217.    putchar('\n');
  218.    while (*mess2) putchar (*mess2++);
  219.    putchar ('\n');
  220.    exit(1);
  221. }
  222.